package servlet;

import dao.PersonDAO;
import entity.PersonBean;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/group/people/personSave")
public class PersonSaveServlet extends HttpServlet {
    public PersonSaveServlet() {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charset=UTF-8");
        String id = req.getParameter("id");
        String person_number = req.getParameter("num");
        String person_name = req.getParameter("name");
        String department = req.getParameter("department");
        String time = req.getParameter("time");
        String degree = req.getParameter("level");
        String major = req.getParameter("major");
        String person_position = req.getParameter("job");
        String telephone = req.getParameter("telephone");
        String address = req.getParameter("address");
        String introduce = req.getParameter("introduce");

        if (!id.equals("-1")){
            PersonDAO.editPerson(id,person_number,person_name,department,time,degree,major,person_position,telephone,address,introduce);
        }
        else {
            PersonDAO.addPerson(person_number,person_name,department,time,degree,major,person_position,telephone,address,introduce);
        }

        resp.setHeader("refresh","0;URL="+"personList");
    }
}
